<?php
class media_admin_http extends controller_prototype_class
{
	private $_mediaMapper;
	private $_imageMapper;

    protected $_page = 1;	
    protected $_media;
    
    protected $actionTitle;

    protected function GetImageMapper()
    {
		if (!$this->_imageMapper) 
		{
			include_once(_APP_PATH. "business/image/image_mapper.php");
			$this->_imageMapper = ImageMapper::GetInstance(); 
		}
		return $this->_imageMapper;     	
    }    
    
    protected function GetMediaMapper()
    {
		if (!$this->_mediaMapper) 
		{
			include_once(_APP_PATH. "business/media/media_mapper.php");
			$this->_mediaMapper = MediaMapper::GetInstance(); 
		}
		return $this->_mediaMapper;     	
    }    

    protected function GetMediaFromQueryId()
    {
		$id = (int)$this->Request()->get('id');	
		if ($id)
		{
			return $this->GetMediaMapper()->GetById($id);
		}

		return false;
    }    
    
    protected function GetQueryPage()
    {
    	if ($this->Request()->hasQuery('page'))
    	{
			$this->_page = (int)$this->Request()->getQuery('page');
			if ($this->_page != $this->Request()->getCookie('page')) 
			{
				$link = MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => $this->GetSelfName()));
				$this->Request()->SetCookie('page', $this->_page, 0, $link);   		
			}
    	}
    	else
    	{
    		$this->_page = (int)$this->Request()->getCookie('page');
    	} 

    	return $this->_page;
    }  

    private $_filter;
    
    protected function GetFilter($prefixName = 'media')
    {
    	if ($this->_filter) return $this->_filter;
    	
		$this->_filter = $this->GetMediaMapper()->GetMediaFilter(); 
		
		$filterTagStoreName = $prefixName. '_filter_tags' .$this->GetSelfName();
		$filterFromDateStoreName = $prefixName. '_filter_from_date' .$this->GetSelfName();
		$filterToDateStoreName = $prefixName. '_filter_to_date' .$this->GetSelfName();
		
		if ($this->Request()->hasQuery('resetmediafilter'))
		{
			$this->Libs()->session->SetValue($filterTagStoreName, '');
			$this->Libs()->session->SetValue($filterFromDateStoreName, '');
			$this->Libs()->session->SetValue($filterToDateStoreName, '');
			return $this->_filter;
		}
		
    	if ($this->Request()->hasQuery('media_filter_tags'))
    	{
			$this->Libs()->session->SetValue($filterTagStoreName, $this->Request()->getQuery('media_filter_tags')); 
    	}

        if ($this->Request()->hasQuery('media_filter_from_date'))
    	{
			$this->Libs()->session->SetValue($filterFromDateStoreName, $this->Request()->getQuery('media_filter_from_date'));
    	}

        if ($this->Request()->hasQuery('media_filter_to_date'))
    	{
			$this->Libs()->session->SetValue($filterToDateStoreName, $this->Request()->getQuery('media_filter_to_date'));
    	}

    	$this->_filter->SetTags(trim($this->Libs()->session->GetValue($filterTagStoreName)));
    	$this->_filter->SetFromDate(trim($this->Libs()->session->GetValue($filterFromDateStoreName)));
    	$this->_filter->SetToDate(trim($this->Libs()->session->GetValue($filterToDateStoreName)));
    	
    	return $this->_filter;
    }

    public function OutMediaFilesBlock($baseUrlParams, $params = array())
    {
        $filter = $this->GetFilter('media_block');       

        $page = (int)$this->Request()->getQuery('media_page');
        $onPage = MozgCfg::GetProp('admin/media//perPage');
               
        $paginator = SysLoader::GetModule('controlls', 'http', '_web')->GetCtrl('paginator');
        $pattern = MozgRoute::Get('http')->Create(array_merge($baseUrlParams, array('media_page' => '__page__')));
        $paginator->SetUrlPattern($pattern);         
        $pagMapper = $paginator->GetMapper();
        $pagMapper->Set(0, $page, $onPage);
        
        $files = $this->GetMediaMapper()->GetMedia($pagMapper, $filter);
        $pagMapper->SetTotalCount($this->GetMediaMapper()->GetTotal());               
    	
        $t = $this->GetTemplator();
        $t->ctrlName = $this->GetSelfName();
        $t->baseUrlParams = $baseUrlParams;
        $t->files = $files;
        $t->mediaControl = SysLoader::GetModule('controlls', 'http', '_web')->GetCtrl('media');
        $t->thumbFolder = $this->GetPhotoThumbFolder('mediathumbnail');
        $t->page = $page;
        $t->pages = $paginator->Generate(); 
		$t->params = $params;
        
        return $t->render('media/_media_block_files.phtml');    		
    }     
    
    protected function InitExtMode()
    {
    	$isExtMode = false;
    	
    	if ($this->Request()->has('ext_id') && $this->Request()->has('ext_mod'))
    	{
    		$this->Libs()->session->SetValue('mediaExtId', (int)$this->Request()->get('ext_id'));
    		$this->Libs()->session->SetValue('mediaExtMod', $this->Request()->get('ext_mod'));
    	}
    	
        if ($this->Libs()->session->GetValue('mediaExtId'))
		{
			if (!is_null(MozgCfg::GetProp('global/media//extMods/' .$this->Libs()->session->GetValue('mediaExtMod'))))
			{
				$mod = MozgCfg::GetProp('global/media//extMods/' .$this->Libs()->session->GetValue('mediaExtMod'));
				$extCtrl = $mod['ctrl'];
				
				if ($extCtrl && $this->_ctrls->$extCtrl && method_exists($this->_ctrls->$extCtrl, 'ValidateExtId'))
				{
					$isExtMode = $this->_ctrls->$extCtrl->ValidateExtId($this->Libs()->session->GetValue('mediaExtId'));
				}
			}
			
			if (!$isExtMode)
			{
	    		$this->Libs()->session->SetValue('mediaExtId', null);
	    		$this->Libs()->session->SetValue('mediaExtMod', null);				
			}
		}      	
    }
    
    protected function GetExtId()
    {
    	return $this->Libs()->session->GetValue('mediaExtId');
    }
    
    protected function GetExtMod()
    {
    	return $this->Libs()->session->GetValue('mediaExtMod');
    }    

    public function GetPhotoThumbFolder($thumbFolder)
    {
        return $this->GetImageMapper()->GetImageThumbFolder($thumbFolder);      
    }     
}    